package org.aspectsense.examples.adaptivemail;

import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.aspectsense.middleware.annotations.AspectSense;
import org.aspectsense.middleware.annotations.Service;
import org.aspectsense.middleware.interfaces.MailingService;


	/**
	 * Implements a basic MIME mailing functionality for sending emails with attachements.
	 * @author Costas Kakousis
	 *
	 */
@AspectSense()
@Service(displayID = "Mailing Service",
		serviceType = org.aspectsense.middleware.interfaces.MailingService.class, 
		utilityFunction = "1-(0.5+0.9)*0.1",
		isEndUserService = false)
	public class MailingServiceImpl implements MailingService{
		
		public MailingServiceImpl (){
			;
		}
		/**
		 * Sends an email using the given mail server, from the given sender, to the given recipient
		 * with the given subject, message and attachments. 
		 * @param mailServer the outgoing mail server
		 * @param from the email address of the sender
		 * @param to the email address of the recipient
		 * @param subject the email's subject
		 * @param messageBody the email's body
		 * @param attachments a string array with the path names of the attached files
		 */
		public void sendMail(String mailServer, String from, String to, String subject, 
				             String messageBody,String[] attachments) {
			// Setup mail server
			Properties props = System.getProperties();
			props.put("mail.smtp.host", mailServer);

			// Get a mail session
			Session session = Session.getDefaultInstance(props, null);

			// Define a new mail message
			Message message = new MimeMessage(session);
			try {
				message.setFrom(new InternetAddress(from));
		
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			message.setSubject(subject);

			// Create a message part to represent the body text
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setText(messageBody);

			//use a MimeMultipart as we need to handle the file attachments
			Multipart multipart = new MimeMultipart();

			//add the message body to the mime message
			multipart.addBodyPart(messageBodyPart);

			if(attachments!=null){
			// add any file attachments to the message
			addAtachments(attachments, multipart);
			}
			// Put all message parts in the message
			message.setContent(multipart);

			// Send the message
			Transport.send(message);
			} catch (AddressException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MessagingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		
		/**
		 * Helper method for including attachments to the MIME multipart
		 * @param attachments the path names for the attached files
		 * @param multipart the MIME multipart where we add the attachment body part
		 * @throws MessagingException
		 * @throws AddressException
		 */
		protected void addAtachments(String[] attachments, Multipart multipart)
		throws MessagingException, AddressException { 
			
			for(int i = 0; i<=attachments.length -1; i++)
			{
				String filename = attachments[i];
				MimeBodyPart attachmentBodyPart = new MimeBodyPart();
				
				//use a JAF FileDataSource as it does MIME type detection
				DataSource source = new FileDataSource(filename);
				attachmentBodyPart.setDataHandler(new DataHandler(source));
				
				//assume that the filename you want to send is the same as the
				//actual file name - could alter this to remove the file path
				attachmentBodyPart.setFileName(filename);
				
				//add the attachment
				multipart.addBodyPart(attachmentBodyPart);
			}
		} 
		
		public static void main (String args[]){
			MailingService ms = new MailingServiceImpl();
			ms.sendMail("cs.ucy.ac.cy", "from", "to", "subject", "messageBody", null);
		}
}
